package com.xwn.utils.core;

import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 集合操作Util
 * @Author duandw
 * @Date 2019/12/31 16:32
 **/
public class CollectionUtilsImpl extends CollectionUtils {
    public CollectionUtilsImpl() {
    }

    public static String joinString(String separator, Collection<?> ls) {
        if (T.StringUtils.isEmpty(separator)) {
            separator = ",";
        }

        if (isEmpty(ls)) {
            return null;
        } else {
            StringBuilder sbr = new StringBuilder();
            Iterator var3 = ls.iterator();

            while(var3.hasNext()) {
                Object oj = var3.next();
                sbr.append(oj.toString());
                sbr.append(separator);
            }

            return sbr.substring(0, sbr.length() - separator.length());
        }
    }

    public static String joinString(Collection<?> ls) {
        return joinString(",", ls);
    }

    public static <E> List<List<E>> splitTo(List<E> objects, int subSize) {
        List<List<E>> lists = new ArrayList();
        int idCount = objects.size();
        int loopTimes = idCount / subSize;
        if (loopTimes * subSize < idCount) {
            ++loopTimes;
        }

        for(int i = 0; i < loopTimes; ++i) {
            int fromIndex = i * subSize;
            int toIndex = (i + 1) * subSize;
            lists.add(objects.subList(fromIndex, toIndex > idCount ? idCount : toIndex));
        }

        return lists;
    }

    public static boolean isNotEmpty(Collection<?> collection) {
        return !isEmpty(collection);
    }

    public static boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }
}
